
'use strict';

import React, {Component} from 'react';

import {
    View,
    Text,
    StyleSheet,
    Platform,
    AppRegistry
} from 'react-native';

import RefreshListView,{RefreshState} from '../../base/widget/RefreshListView';
import DiscoverCell from './DiscoverCell';
import {PAGE_SIZE} from "../../base/Constants";
import DiscoverApi from "./api/DiscoverApi"
import BaseScene from "../../base/BaseScene";

let _page = 1;//当前第几页
let isReload = true;

export default class DiscoverScene extends BaseScene {

    constructor(props) {
        super(props);

        this.state = {
            dataList: [],
            response:'',
            refreshState: RefreshState.Idle,
        }
    }

    componentDidMount () {
        this._onHeaderRefresh();
    }


    _onHeaderRefresh = () => {
        this.setState({refreshState: RefreshState.HeaderRefreshing})
        isReload = true
        _page = 1;
        this._getDataList();
    }

    _onFooterRefresh = () => {
        this.setState({refreshState: RefreshState.FooterRefreshing})
        isReload = false
        _page = _page+1;
        this._getDataList();
    }

    _requestSuccess= (response) =>{
        let backList = response.obj;
        let newList = isReload ? backList : [...this.state.dataList, ...backList];
        this.setState({
            response: response,
            dataList : newList,
            refreshState: backList.length<10 ? RefreshState.NoMoreData : RefreshState.Idle
        })
    }

    // 获取测试数据
    _getDataList(){
        let params = {'cityName':'杭州','page':_page,'size':PAGE_SIZE}
        this._fetchRequest(DiscoverApi.getDiscoverList,params);
    }

    _keyExtractor = (item, index) => {
        return index;
    }

    _renderCell = (info) => {
        return <DiscoverCell info={info.item} />
    }

    render() {
        return (
            <View style={styles.container}>
                <RefreshListView
                    data={this.state.dataList}
                    keyExtractor={this._keyExtractor}
                    renderItem={this._renderCell}
                    refreshState={this.state.refreshState}
                    onHeaderRefresh={this._onHeaderRefresh}
                    onFooterRefresh={this._onFooterRefresh}

                    // 可选
                    footerRefreshingText= '玩命加载中 >.<'
                    footerFailureText = '我擦嘞，居然失败了 =.=!'
                    footerNoMoreDataText= '-我是有底线的-'
                />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        marginTop: Platform.OS == 'ios' ? 20 : 0,
    },
    title: {
        fontSize: 18,
        height: 84,
        textAlign: 'center'
    }
})